package com.app.campusguide;

import java.util.ArrayList;
import com.app.sqlite_connect.HandlerSqlite;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TableRow.LayoutParams;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.graphics.Color;


public class View_schedule_activity extends Activity {
	private final String APP_LOG = "View_schedule_activity";
	private HandlerSqlite database;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_schedule);
		database = new HandlerSqlite(this);
		initDayOfWeekSpinner();
		ImageView btn_schedule_home =(ImageView) findViewById(R.id.img_btn_schedule_home);
		btn_schedule_home.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	Intent i = new Intent(getApplicationContext(),AcivityMain.class);
        		startActivity(i);
            }      
        });
	}
	
	/**
	 * Fill days of week spinner to select day to query
	 */
	private void initDayOfWeekSpinner()
	{
		Spinner spn_day_of_week = (Spinner)findViewById(R.id.spn_schedule_day_of_week);
		ArrayAdapter<?> adp_day_of_week_adapter = ArrayAdapter.createFromResource(this, R.array.days_name,android.R.layout.simple_spinner_item);
		adp_day_of_week_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spn_day_of_week.setAdapter(adp_day_of_week_adapter);
		spn_day_of_week.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id){
				//Get id of day selected
				String str_day_of_week = ((Long)parentView.getItemIdAtPosition(position)).toString();
				//Query for class to take this day
				ArrayList<Class_view> classes_to_take_for_day = database.queryScheduleFor(str_day_of_week);
				//Query for class to take now
				ArrayList<Class_view> classes_to_take_now = database.queryClassNowView();
				//Query for class to take next
				ArrayList<Class_view> classes_to_take_next = database.queryNextClassView();
				//Fill with data
				fillScheduleFor(classes_to_take_for_day);
				fillClassNow(classes_to_take_now);
				fillClassNext(classes_to_take_next);
			}
			
			public void onNothingSelected(AdapterView<?> parentView){
			}	
		});
	}
	
	/**
	 * Fill table with information of classes for day selected
	 * @param classes_to_take_for_day List of classes 
	 */
	private void fillScheduleFor(ArrayList<Class_view> classes_to_take_for_day)	{
		TableLayout tbl_lyt_schedule_detail = (TableLayout)findViewById(R.id.table_layout_schedule_detail );
		tbl_lyt_schedule_detail.removeAllViews();
		TableRow tbl_row_init = new TableRow(getApplicationContext());
		tbl_row_init.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		//TextView for class name
		TextView tvw_1 = new TextView(getApplicationContext());
		tvw_1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_1.setGravity(Gravity.CENTER);
		tvw_1.setTextColor(Color.BLACK);
		tvw_1.setText(R.string.str_sch_class_name);
		//TextView for start time
		TextView tvw_2 = new TextView(getApplicationContext());
		tvw_2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_2.setGravity(Gravity.CENTER);
		tvw_2.setTextColor(Color.BLACK);
		tvw_2.setText(R.string.str_sch_init_time);
		//TextView for end time
		TextView tvw_3 = new TextView(getApplicationContext());
		tvw_3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_3.setGravity(Gravity.CENTER);
		tvw_3.setTextColor(Color.BLACK);
		tvw_3.setText(R.string.str_sch_end_time);
		//TextView for class hall name
		TextView tvw_4 = new TextView(getApplicationContext());
		tvw_4.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_4.setGravity(Gravity.CENTER);
		tvw_4.setTextColor(Color.BLACK);
		tvw_4.setText(R.string.str_sch_hall_class);
		//Add view to table row
		tbl_row_init.addView(tvw_1);
		tbl_row_init.addView(tvw_2);
		tbl_row_init.addView(tvw_3);
		tbl_row_init.addView(tvw_4);
		//Add table row to table layout
		tbl_lyt_schedule_detail.addView(tbl_row_init,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		//Load data from result query
		for(int i=0; i < classes_to_take_for_day.size(); i++){
			TableRow tbl_row = new TableRow(getApplicationContext());
			tbl_row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			TextView tvw_class_name = new TextView(getApplicationContext());
			tvw_class_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_class_name.setGravity(Gravity.LEFT);
			tvw_class_name.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			if(classes_to_take_for_day.get(i).getStr_class_name().length()>=20){
				tvw_class_name.setText(classes_to_take_for_day.get(i).getStr_class_name().substring(0,19));
			}else{
				tvw_class_name.setText(classes_to_take_for_day.get(i).getStr_class_name());
			}
			TextView tvw_init_time = new TextView(getApplicationContext());
			tvw_init_time.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_init_time.setGravity(Gravity.CENTER);
			tvw_init_time.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			tvw_init_time.setText(classes_to_take_for_day.get(i).getStr_start_hour());
			
			TextView tvw_end_time = new TextView(getApplicationContext());
			tvw_end_time.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_end_time.setGravity(Gravity.CENTER);
			tvw_end_time.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			tvw_end_time.setText(classes_to_take_for_day.get(i).getStr_end_hour());
			
			TextView tvw_hall = new TextView(getApplicationContext());
			tvw_hall.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_hall.setGravity(Gravity.CENTER);
			tvw_hall.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			tvw_hall.setText(classes_to_take_for_day.get(i).getStr_site_name());
			
			tbl_row.addView(tvw_class_name);
			tbl_row.addView(tvw_init_time);
			tbl_row.addView(tvw_end_time);
			tbl_row.addView(tvw_hall);					
			tbl_lyt_schedule_detail.addView(tbl_row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			}
	}
	
	/**
	 * Fill table with information of class to take now
	 * @param classes_to_take_now Data of class
	 */
	public void fillClassNow(ArrayList<Class_view> classes_to_take_now)	{
		TableLayout tbl_lyt_class_now = (TableLayout)findViewById(R.id.table_layout_class_now_detail);
		tbl_lyt_class_now.removeAllViews();
		TableRow tbl_row_init = new TableRow(getApplicationContext());
		tbl_row_init.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		//TextView for class name
		TextView tvw_1 = new TextView(getApplicationContext());
		tvw_1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_1.setGravity(Gravity.CENTER);
		tvw_1.setTextColor(Color.BLACK);
		tvw_1.setText(R.string.str_sch_class_name);
		//TextView for start time
		TextView tvw_2 = new TextView(getApplicationContext());
		tvw_2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_2.setGravity(Gravity.CENTER);
		tvw_2.setTextColor(Color.BLACK);
		tvw_2.setText(R.string.str_sch_init_time);
		//TextView for end time
		TextView tvw_3 = new TextView(getApplicationContext());
		tvw_3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_3.setGravity(Gravity.CENTER);
		tvw_3.setTextColor(Color.BLACK);
		tvw_3.setText(R.string.str_sch_end_time);
		//TextView for class hall name
		TextView tvw_4 = new TextView(getApplicationContext());
		tvw_4.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_4.setGravity(Gravity.CENTER);
		tvw_4.setTextColor(Color.BLACK);
		tvw_4.setText(R.string.str_sch_hall_class);
		//Add view to table row
		tbl_row_init.addView(tvw_1);
		tbl_row_init.addView(tvw_2);
		tbl_row_init.addView(tvw_3);
		tbl_row_init.addView(tvw_4);
		//Add table row to table layout
		tbl_lyt_class_now.addView(tbl_row_init,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		//Load data from result query
		for(int i=0; i < classes_to_take_now.size(); i++){
			TableRow tbl_row = new TableRow(getApplicationContext());
			tbl_row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			TextView tvw_class_name = new TextView(getApplicationContext());
			tvw_class_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_class_name.setGravity(Gravity.LEFT);
			tvw_class_name.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			if(classes_to_take_now.get(i).getStr_class_name().length()>=20){
				tvw_class_name.setText(classes_to_take_now.get(i).getStr_class_name().substring(0,19));
			}else{
				tvw_class_name.setText(classes_to_take_now.get(i).getStr_class_name());
			}

			TextView tvw_init_time = new TextView(getApplicationContext());
			tvw_init_time.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_init_time.setGravity(Gravity.CENTER);
			tvw_init_time.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			tvw_init_time.setText(classes_to_take_now.get(i).getStr_start_hour());
			
			TextView tvw_end_time = new TextView(getApplicationContext());
			tvw_end_time.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_end_time.setGravity(Gravity.CENTER);
			tvw_end_time.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			tvw_end_time.setText(classes_to_take_now.get(i).getStr_end_hour());
			
			TextView tvw_hall = new TextView(getApplicationContext());
			tvw_hall.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_hall.setGravity(Gravity.CENTER);
			tvw_hall.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			tvw_hall.setText(classes_to_take_now.get(i).getStr_site_name());
			
			tbl_row.addView(tvw_class_name);
			tbl_row.addView(tvw_init_time);
			tbl_row.addView(tvw_end_time);
			tbl_row.addView(tvw_hall);
			
			tbl_lyt_class_now.addView(tbl_row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		}
	}
	
	/**
	 * Fill table with class to take next
	 * @param classes_to_take_next Data of next class
	 */
	public void fillClassNext(ArrayList<Class_view> classes_to_take_next) {
		TableLayout tbl_lyt_class_next = (TableLayout)findViewById(R.id.table_layout_class_next_detail);
		tbl_lyt_class_next.removeAllViews();
		TableRow tbl_row_init = new TableRow(getApplicationContext());
		tbl_row_init.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		//TextView for class name
		TextView tvw_1 = new TextView(getApplicationContext());
		tvw_1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_1.setGravity(Gravity.CENTER);
		tvw_1.setTextColor(Color.BLACK);
		tvw_1.setText(R.string.str_sch_class_name);
		//TextView for start time
		TextView tvw_2 = new TextView(getApplicationContext());
		tvw_2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_2.setGravity(Gravity.CENTER);
		tvw_2.setTextColor(Color.BLACK);
		tvw_2.setText(R.string.str_sch_init_time);
		//TextView for end time
		TextView tvw_3 = new TextView(getApplicationContext());
		tvw_3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_3.setGravity(Gravity.CENTER);
		tvw_3.setTextColor(Color.BLACK);
		tvw_3.setText(R.string.str_sch_end_time);
		//TextView for class hall name
		TextView tvw_4 = new TextView(getApplicationContext());
		tvw_4.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tvw_4.setGravity(Gravity.CENTER);
		tvw_4.setTextColor(Color.BLACK);
		tvw_4.setText(R.string.str_sch_hall_class);
		//Add view to table row
		tbl_row_init.addView(tvw_1);
		tbl_row_init.addView(tvw_2);
		tbl_row_init.addView(tvw_3);
		tbl_row_init.addView(tvw_4);
		//Add table row to table layout
		tbl_lyt_class_next.addView(tbl_row_init,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		//Load data from result query
		for(int i=0; i < classes_to_take_next.size(); i++){
			TableRow tbl_row = new TableRow(getApplicationContext());
			tbl_row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			TextView tvw_class_name = new TextView(getApplicationContext());
			tvw_class_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_class_name.setGravity(Gravity.LEFT);
			tvw_class_name.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			if(classes_to_take_next.get(i).getStr_class_name().length()>=20){
				tvw_class_name.setText(classes_to_take_next.get(i).getStr_class_name().substring(0,19));
			}else{
				tvw_class_name.setText(classes_to_take_next.get(i).getStr_class_name());
			}

			
			TextView tvw_init_time = new TextView(getApplicationContext());
			tvw_init_time.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_init_time.setGravity(Gravity.CENTER);
			tvw_init_time.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			tvw_init_time.setText(classes_to_take_next.get(i).getStr_start_hour());
			
			TextView tvw_end_time = new TextView(getApplicationContext());
			tvw_end_time.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_end_time.setGravity(Gravity.CENTER);
			tvw_end_time.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			tvw_end_time.setText(classes_to_take_next.get(i).getStr_end_hour());
			
			TextView tvw_hall = new TextView(getApplicationContext());
			tvw_hall.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvw_hall.setGravity(Gravity.CENTER);
			tvw_hall.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
			tvw_hall.setText(classes_to_take_next.get(i).getStr_site_name());
			
			tbl_row.addView(tvw_class_name);
			tbl_row.addView(tvw_init_time);
			tbl_row.addView(tvw_end_time);
			tbl_row.addView(tvw_hall);
			tbl_lyt_class_next.addView(tbl_row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		}
	}
}
